package com.example.demo2.controller;

import com.example.demo2.biz.Answerbiz;
import com.example.demo2.biz.Userbiz;
import com.example.demo2.entity.User;
import com.example.demo2.entity.answerRecord;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("Answer")
public class AnswerController {
    private @Autowired
    Answerbiz answerbiz;
    private @Autowired
    Userbiz userbiz;
    @RequestMapping("saveAnswer")
    public answerRecord saveAnswer(String openID, String correctCount, String errorCount, String score){
        String result = "";
        Date time = new Date();
         new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
        Timestamp ts = new Timestamp(time.getTime());
        int correctCount1 = Integer.parseInt(correctCount);
        int errorCount1 = Integer.parseInt(errorCount);
                        int score1 = Integer.parseInt(score);
        answerRecord answerRecord = new answerRecord(openID,correctCount1,errorCount1,score1,ts);
        try {
            answerbiz.saveanswer(answerRecord );
            result = "插入成功";
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result = "插入失败";
        }
        return answerRecord ;

    }
    @RequestMapping("selectAnswer")
    public List<answerRecord> selectAnswer(String openID){

        String result = "";

        List<answerRecord> answerrecord=null;
        try {
             answerrecord=answerbiz.getanswerRecord(openID);
            result = "查询成功";
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result = "查询失败";
        }
        System.out.println(result);

        return  answerrecord ;

    }
    @RequestMapping("selectRank")
    public String selectRank(String openID){

        String result = "";
        String json ="";
        List<answerRecord> answerrecord=null;

        try {
            answerrecord=answerbiz.getrank();
            int count = answerrecord.size();
            for(int i=0;i<count;i++){

                   User user=userbiz.getUser(answerrecord.get(i).getOpenID()) ;
//                System.out.println(openID);
//                System.out.println(user.getOpenID());
                   int k =i+1;
//                String json = "{'name': 'helloworlda','array':[{'a':'111','b':'222','c':'333'},{'a':'999'}],'address':'111','people':{'name':'happ','sex':'girl'}}";
                if(i==(count-1))
                {
                    if (user.getOpenID().compareTo(openID)==0){
                        json ="{ \"openID\":\""+user.getOpenID()+"\",\"nickName\":\""+user.getNickName()+"\",\"avatarUrl\":\""+user.getAvatarUrl()+"\",\"score\":"+answerrecord.get(i).getScore()+",\"rank\":"+k+"},"+json;
                        System.out.println(json);
                    }

                   json +="{ \"openID\":\""+user.getOpenID()+"\",\"nickName\":\""+user.getNickName()+"\",\"avatarUrl\":\""+user.getAvatarUrl()+"\",\"score\":"+answerrecord.get(i).getScore()+",\"rank\":"+k+"}";
                }
                else {

                    json += "{ \"openID\":\"" + user.getOpenID() + "\",\"nickName\":\"" + user.getNickName() + "\",\"avatarUrl\":\"" + user.getAvatarUrl() + "\",\"score\":" + answerrecord.get(i).getScore() + ",\"rank\":" + k + "},";
                    if (user.getOpenID().compareTo(openID)==0) {
                        json = "{ \"openID\":\"" + user.getOpenID() + "\",\"nickName\":\"" + user.getNickName() + "\",\"avatarUrl\":\"" + user.getAvatarUrl() + "\",\"score\":" + answerrecord.get(i).getScore() + ",\"rank\":" + k + "}," + json;


                    }
                    else {

                    }

                }




            }
            result = "查询成功";
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result = "查询失败";
        }
        json ="["+json+"]";



       System.out.println(JSONArray.fromString(json).toString());
        return  JSONArray.fromString(json).toString();

    }

}
